package net.hn.hnms.biz.common.activiti.utils;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.StrUtil;
import org.activiti.bpmn.model.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;

import static cn.hutool.core.util.NumberUtil.isInteger;

/**
 * Created on 2022/8/1.
 *
 * @author ly
 * @desc:
 */
public class ProcessUtil {

    /**
     * 获得正则表达式的key
     *
     * @param param
     * @return
     */
    public static Map<String, Object> getCountersignParameters(String param) {
        Map<String, Object> params = new HashMap<>();
        if (StrUtil.isBlank(param)) {
            return params;
        }
        boolean containsEquals = StrUtil.contains(param, "==");
        param = param.replace("#{", "").replace("${", "").replace("}", "");
        char[] res = param.toCharArray();
        ArrayBlockingQueue<Character> s = new ArrayBlockingQueue<>(500);
        for (int i = 0; i < res.length; i++) {
            char c = res[i];
            if (c != '>' && c != '<' && c != '=' && c != '&' && c != '|' && !isInteger(String.valueOf(c))) {
                s.add(c);
            } else {
                StringBuilder temp = new StringBuilder();
                while (!s.isEmpty()) {
                    temp.append(s.poll());
                }
                if (StrUtil.isNotEmpty(temp.toString())) {
                    if (containsEquals) {
                        String value = StrUtil.trim(param.substring(param.indexOf("==") + 2));
                        Boolean isBoolean = value.equals("true") || value.equals("false");
                        params.put(temp.toString(), Validator.isNumber(value) ? Integer.valueOf(value) : (isBoolean ? BooleanUtil.toBoolean(value) : value));
                    } else {
                        params.put(temp.toString(), "");
                    }
                }
            }
        }
        return params;
    }

    /*连线*/
    public static SequenceFlow createSequenceFlow(String from, String to, String name, String conditionExpression) {
        SequenceFlow flow = new SequenceFlow();
        flow.setSourceRef(from);
        flow.setTargetRef(to);
        flow.setName(name);
        if (StrUtil.isNotEmpty(conditionExpression)) {
            flow.setConditionExpression(conditionExpression);
        }
        return flow;
    }

    //排他网关
    public static ExclusiveGateway createExclusiveGateway(String id, String name) {
        ExclusiveGateway exclusiveGateway = new ExclusiveGateway();
        exclusiveGateway.setId(id);
        exclusiveGateway.setName(name);
        return exclusiveGateway;
    }

    //并行网关
    public static ParallelGateway createParallelGateway(String id, String name) {
        ParallelGateway gateway = new ParallelGateway();
        gateway.setId(id);
        gateway.setName(name);
        return gateway;
    }

    //开始节点
    public static StartEvent createStartEvent() {
        StartEvent startEvent = new StartEvent();
        startEvent.setId("startEvent");
        return startEvent;
    }

    /*结束节点*/
    public static EndEvent createEndEvent() {
        EndEvent endEvent = new EndEvent();
        endEvent.setId("endEvent");
        return endEvent;
    }

}
